﻿//====================================================================
// Create by Daniel
// file： ConfigUtil.cs
// project ：Utility Class
// Build time：2012-8-20
// modify:
// description:
// ===================================================================

using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data.OleDb;
using System.Data;
using System.Collections;
using System.Xml;
using System.Configuration;
using System.Web;


namespace YunCMS.Common
{
    /// <summary>
    /// 有关WebConfig的UtilClass
    /// </summary>
    public class ConfigUtil
    {
        public ConfigUtil()
        { }

        private static XmlDocument GetWebConfig()
        {
            XmlDocument document1 = new XmlDocument();
            document1.Load(GetXmlFilePath());
            return document1;
        }

        private static string GetXmlFilePath()
        {
            return HttpContext.Current.Server.MapPath("~/Web.config");
        }

        /// <summary>
        /// 通过程序设置WebConfig中的Appsetting里面的Value值
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public static void SetValue(string key, string value)
        {
            Exception exception1;
            XmlDocument document1 = GetWebConfig();
            XmlNode node1 = document1.SelectSingleNode("/appSettings");
            node1 = document1.SelectSingleNode("//appSettings");
            if (node1 == null)
            {
                throw new InvalidOperationException("appSettings section not found");
            }
            try
            {
                XmlElement element1 = (XmlElement)node1.SelectSingleNode("//add[@key='" + key + "']");
                if (element1 != null)
                {
                    element1.SetAttribute("value", value);
                }
                else
                {
                    XmlElement element2 = document1.CreateElement("add");
                    element2.SetAttribute("key", key);
                    element2.SetAttribute("value", value);
                    node1.AppendChild(element2);
                }
                try
                {
                    XmlTextWriter writer1 = new XmlTextWriter(GetXmlFilePath(), null);
                    writer1.Formatting = Formatting.Indented;
                    document1.WriteTo(writer1);
                    writer1.Flush();
                    writer1.Close();
                }
                catch (Exception exception2)
                {
                    exception1 = exception2;
                    throw new Exception(exception1.Message);
                }
            }
            catch (Exception exception3)
            {
                exception1 = exception3;
                throw new InvalidOperationException(exception1.Message);
            }
        }

        /// <summary>
        /// 是否存在键值 Web.Config
        /// </summary>
        /// <param name="strNodeName"></param>
        /// <param name="strKey"></param>
        /// <returns></returns>
        public bool IsExistKey(string strNodeName, string strKey)
        {
            XmlDocument document1 = GetWebConfig();
            bool flag1 = false;
            XmlNodeList list1 = document1.DocumentElement.ChildNodes;
            foreach (XmlElement element1 in list1)
            {
                if (element1.Name == strNodeName)
                {
                    XmlNodeList list2 = element1.ChildNodes;
                    if (list2.Count > 0)
                    {
                        foreach (XmlElement element2 in list2)
                        {
                            flag1 = element2.Attributes["key"].InnerXml == strKey;
                            if (flag1)
                            {
                                break;
                            }
                        }
                    }
                }
            }
            return flag1;
        }

        /// <summary>
        /// 是否存在节点 Web.Config
        /// </summary>
        /// <param name="strNode"></param>
        /// <returns></returns>
        public bool IsExistNode(string strNode)
        {
            XmlDocument document1 = GetWebConfig();
            XmlNode node1 = document1.SelectSingleNode("/" + strNode);
            node1 = document1.SelectSingleNode("//" + strNode);
            return (node1 != null);
        }
    }
}
